<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    myAxios = (options) => {
      const { url, method = 'get', data } = options
      return new Promise((resolve, reject) => {
        // 1.实例化异步对象
        const xhr = new XMLHttpRequest()
        // 2.设置请求的 地址 和方法
        // xhr.open('get', 'http://ajax-api.itheima.net/api/news')
        xhr.open(method, url)
        xhr.setRequestHeader('content-type',
          'application/json')
        // 3.注册回调函数 服务器响应内容回来之后触发
        xhr.addEventListener('load', () => {
          // console.log(xhr.response)
          resolve(JSON.parse(xhr.response))
        })
        // 4.发送请求
        xhr.send(JSON.stringify(data))
      })
    }
    myAxios({
      url: 'http://ajax-api.itheima.net/api/books',
      method: 'post',
      data: {
        bookname: '三国',
        author: '23453',
        publisher: '2353425',
      }
    }).then((res) => {
      console.log(res)
    })
  </script>
</body>

</html>